package com.example.myapplication.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class f6 extends Fragment {

    private Spinner spinner1;
    private Spinner spinner2;
    View view;
    Context context;
    String ee[]=new String[]{"java","水浒传","西游记","三国演义"};
    /*
    ArrayAdapter适用于条目当中一个文本的内容发生变化
    SimpleAdapter图片和文字都是本地写好的，并不会发生变化，也不需要网络权限
    BaseAdapter的子类，自定义的
    CursorAdapter：游标适配器，用户数据库当中的存储的内容展示在适配器图中
     */
    ArrayAdapter<String>arrayAdapter1;

    List<List<String>>lists=new ArrayList<>();
    List<String>list1=new ArrayList<>();
    List<String>list2=new ArrayList<>();
    List<String>list3=new ArrayList<>();
    List<String>list4=new ArrayList<>();
    public f6() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_f6, container, false);
        context=getContext();
        initView();
        initData();
        x();
        return view;
    }
    /*
    监听spinner
     */

    public void x(){

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String cc=spinner1.getSelectedItem().toString();
                Toast.makeText(context, cc, Toast.LENGTH_SHORT).show();
                switch (position){
                    case 0:
                        arrayAdapter1=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,lists.get(0));
                        break;
                    case 1:
                        arrayAdapter1=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,lists.get(1));
                        break;
                    case 2:
                        arrayAdapter1=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,lists.get(2));
                        break;
                    case 3:
                        arrayAdapter1=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,lists.get(3));
                        break;
                }
                spinner2.setAdapter(arrayAdapter1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
    /*
    初始化数据
     */
    void  initData(){
        list1.add("面向对象");
        list1.add("集合与异常");
        list1.add("正则表达式");
        list1.add("多线程");
        lists.add(list1);
        list2.add("宋江");
        list2.add("林冲");
        list2.add("李魁");
        list2.add("燕青");
        lists.add(list2);
        list3.add("孙悟空");
        list3.add("猪八戒");
        list3.add("沙和尚");
        list3.add("白龙马");
        lists.add(list3);
        list4.add("曹操");
        list4.add("郭嘉");
        list4.add("马超");
        list4.add("典韦");
        lists.add(list1);
    }
/*
初始化控件
 */
    private void initView() {
        spinner1 = (Spinner) view.findViewById(R.id.spinner1);
        spinner2 = (Spinner) view.findViewById(R.id.spinner2);
        arrayAdapter1=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,ee);
        spinner1.setAdapter(arrayAdapter1);
    }
}
